package app.goods.info.utils;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;

import app.goods.info.constants.GlobalCache;
import app.goods.info.constants.UrlConstont;
import app.goods.info.services.domain.GoodsInfo;
import app.goods.info.services.domain.ShopInfo;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;

public class DealWithLocationForShop {
	static String token = "B01E6A47BB00C9D79E0DF18E84FD3E40";

	public static void main(String[] args) throws InterruptedException {
//		queryLocationForAllShop();
		queryLocationForOneShop("754");
	}

	private static void queryLocationForOneShop(String shopNo) throws InterruptedException {
		// TODO Auto-generated method stub
		GlobalCache.getInstance().setLoginToken(token);
		GlobalCache.getInstance().initAllCache();
		double[] bearings = { 0, 45, 90, 135, 180, 225, 270, 315 };

		DecimalFormat df = new DecimalFormat("#.######");

		List<ShopInfo> allShops = GlobalCache.getInstance().getAllShopInfos().stream()
				.filter(shopInfo -> shopInfo.getShopNo().equals(shopNo)).collect(Collectors.toList());
		for (ShopInfo shopInfo : allShops) {

			System.out.println(shopInfo.getShopName() + "======== shopNo: " + shopInfo.getShopNo() + "   index:"
					+ allShops.indexOf(shopInfo));

			List<GoodsInfo> result = getGoodsForShop(shopInfo);
			Thread.sleep(600);
			if (result.size() > 0) {
				System.out.println("can get data");
				continue;
			}
			jumpOutHere: for (int i = 0; i < 10; i++) {
				for (double bearing : bearings) {
					double[] newCoordinate = GeoUtil.calculateNewCoordinate(shopInfo.getLatitude(),
							shopInfo.getLongitude(), 500 * i, bearing);
					shopInfo.setLatitude(newCoordinate[0]);
					shopInfo.setLongitude(newCoordinate[1]);
					result = getGoodsForShop(shopInfo);
					Thread.sleep(600);
					if (result.size() > 0) {
						System.out.println(shopInfo.getShopName() + "======== shopNo: " + shopInfo.getShopNo()
								+ " location:" + shopInfo.getLatitude() + "," + shopInfo.getLongitude());
						break jumpOutHere;
					}
				}

			}

		}
	}

	private static void queryLocationForAllShop() throws InterruptedException {
		GlobalCache.getInstance().setLoginToken(token);
		GlobalCache.getInstance().initAllCache();
		double[] bearings = { 0, 45, 90, 135, 180, 225, 270, 315 };

		DecimalFormat df = new DecimalFormat("#.######");

		List<ShopInfo> allShops = GlobalCache.getInstance().getAllShopInfos().stream()
				.filter(shopInfo -> shopInfo.getStatus().equals("ON")).collect(Collectors.toList());
		for (ShopInfo shopInfo : allShops) {

			System.out.println(shopInfo.getShopName() + "======== shopNo: " + shopInfo.getShopNo() + "   index:"
					+ allShops.indexOf(shopInfo));

			List<GoodsInfo> result = getGoodsForShop(shopInfo);
			Thread.sleep(600);
			if (result.size() > 0) {
				continue;
			}
			jumpOutHere: for (int i = 0; i < 10; i++) {
				for (double bearing : bearings) {
					double[] newCoordinate = GeoUtil.calculateNewCoordinate(shopInfo.getLatitude(),
							shopInfo.getLongitude(), 500 * i, bearing);
					shopInfo.setLatitude(newCoordinate[0]);
					shopInfo.setLongitude(newCoordinate[1]);
					result = getGoodsForShop(shopInfo);
					Thread.sleep(600);
					if (result.size() > 0) {
						System.out.println(shopInfo.getShopName() + "======== shopNo: " + shopInfo.getShopNo()
								+ " location:" + shopInfo.getLatitude() + "," + shopInfo.getLongitude());
						break jumpOutHere;
					}
				}

			}

		}
	}

	public static List<GoodsInfo> getGoodsForShop(ShopInfo shopInfo) {
		List<GoodsInfo> result = new ArrayList<GoodsInfo>();
		String queryUrl = UrlConstont.CENTRALIZED_PROCUREMENT_GOODS_SHOP.concat("1" + "?");
		queryUrl = UrlUtils.getRequestUrl(queryUrl);
		queryUrl = queryUrl.concat("catalogue_no=").concat("2514").concat("&shop_no=").concat(shopInfo.getShopNo())
				.concat("&search_mode=1&sort_type=10&coordinate=").concat(String.valueOf(shopInfo.getLatitude()))
				.concat(",").concat(String.valueOf(shopInfo.getLongitude()));
		String responseBody = HttpClientUtils.get(queryUrl, UrlUtils.builQueryParameters());
		if (StringUtils.isNotEmpty(responseBody)) {
			String messageCode = JSONUtil.parseObj(responseBody).getStr("code");
			if ("0".equals(messageCode)) {
				JSONArray allGoodsArray = JSONUtil.parseObj(responseBody).getJSONObject("result").getJSONArray("list");
				if (allGoodsArray != null && allGoodsArray.size() > 0) {
					result.addAll(JSONUtil.toList(allGoodsArray, GoodsInfo.class));

				}
			}
		}
		System.out.println(queryUrl);
		return result;
	}
}
